import json
import random
import requests

class SendMessage():
    def __init__(self):
        self.appID = "*************"
        self.appsecret = "*********"
        self.access_token = self.get_access_token()
        # self.opend_ids = self.get_openid()
        self. opend_ids=['****','*****']
        self.headers = {
            'Content-Type': 'application/json',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',

        }

    def get_color(self):
        # 获取随机颜色
        get_colors = lambda n: list(map(lambda i: "#" + "%06x" % random.randint(0, 0xFFFFFF), range(n)))
        color_list = get_colors(100)
        return random.choice(color_list)

    def get_access_token(self):
        """
        获取微信公众号的access_token值
        """
        url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.\
            format(self.appID, self.appsecret)
        print(url)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'
        }
        response = requests.get(url, headers=headers).json()
        access_token = response.get('access_token')
        print(access_token)
        return access_token

    def sendmsg(self,data,tu):
        url1 ="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}".format(self.access_token)
        if self.opend_ids != '':
            for open_id in self.opend_ids:
                data1 = {
                    "touser": open_id,
                    "template_id": "*********",
                    "url": "http://weixin.qq.com/download",
                    "topcolor": "#FF0000",
                    "data": {
                        "city": {
                            "value": data[0],
                            "color": self.get_color()
                        },
                        "date": {
                            "value": data[1],
                            "color": self.get_color()
                        },
                        "weather": {
                            "value": data[2],
                            "color":self.get_color()
                        },
                        "low": {
                            "value": data[3],
                            "color": self.get_color()
                        },
                        "high": {
                            "value": data[4],
                            "color": self.get_color()
                        },
                        "tu": {
                            "value": tu,
                            "color": self.get_color()
                        }

                    }
                }
                print(data1)
                response=requests.post(url1, headers=self.headers, json=data1).json()
                print(response)

    def get_weather(self):
        city="重庆"
        url = "https://v0.yiketianqi.com/api?unescape=1&version=v91&appid=43656176&appsecret=I42og6Lm&ext=&cityid=&city={}".format(city)
        res = json.loads(requests.get(url=url, headers=self.headers).text)
        date = res['data'][0]['date'] + " " + res['data'][0]['week']
        weather = res['data'][0]['wea']
        low = res['data'][0]['tem'] + "℃"
        high = res['data'][0]['tem1'] + "℃"
        return city, date, weather, low, high

    def get_tu(self):
        token="********"
        url = "https://v2.alapi.cn/api/qinghua"
        payload = "token={}&format=json".format(token)
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'
        }
        res = json.loads(requests.request("POST", url, data=payload, headers=headers).text)
        tu = res['data']['content']
        print(tu)
        return tu

if __name__ == "__main__":
    sends = SendMessage()
    data=sends.get_weather()
    tu=sends.get_tu()
    sends.sendmsg(data,tu)